/*
 * Created on 2005-4-21
 */
package com.jycrm.presentation.components.paginationSubmit;

/**
 * @author ray
 */
public class PageModel {
	private int pageSize;
	private int nextPageNum;
	private int prevPageNum;
	private int currPage;
	private int totalRow;
	private int pageCount;
	private int first;
	
	/**
	 * @return Returns the first.
	 */
	public int getFirst() {
		return first;
	}
	/**
	 * @param first The first to set.
	 */
	public void setFirst(int first) {
		this.first = first;
	}
    public boolean getHasFirstPage() {
        return (getCurrPage() > 1) ? true : false;
    }

	public boolean getHasNextPage()
	{
		return (getCurrPage() < getPageCount()) ? true : false;
	}

	public boolean getHasPrevPage()
	{
		return (getCurrPage() > 1) ? true : false;
	}

	public boolean getHasLastPage() {
		return (getPageCount() > getCurrPage()) ? true : false;
	}

	public int getNextPageNum()
	{
		return nextPageNum;
	}

	public int getPageSize()
	{
		//return pageSize;
		return (pageSize == 0) ? 10 : pageSize;
	}

	public int getCurrPage()
	{
	  	if(getTotalRow() == 0) {
			return (currPage == 0) ? 0 : currPage;	
	  	} else {
			return (currPage == 0) ? 1 : currPage;	
	  	}
		
	}

	public int getPrevPageNum()
	{
		return prevPageNum;
	}

	public void setNextPageNum(int nextPageNum)
	{
		this.nextPageNum = nextPageNum;
	}

	public void setPageSize(int pageSize)
	{
		this.pageSize = pageSize;
	}

	public void setCurrPage(int currPage)
	{
		//DebugSystemOut.debug("<<<"+toString());
		this.currPage = changePage(currPage, getPageCount());
		//DebugSystemOut.debug(">>>"+toString());
	}

	public void setPrevPageNum(int prevPageNum)
	{
		this.prevPageNum = prevPageNum;
	}

	public int getTotalRow()
	{
		return totalRow;
	}

	public int getPageCount()
	{
		if (pageCount != 0)
		{
			return pageCount;
		}
		else
		{
			return (int) Math.ceil(((double) getTotalRow() / (double) getPageSize()));
		}
	}


	public void setTotalRow(int totalRow)
	{
		this.totalRow = totalRow;
	}

	public void setPageCount(int pageCount)
	{
		this.pageCount = pageCount;
	}

	private static int changePage(int page, int pCount)
	{
		if (page < 2)
		{
			return 1;
		}
		else
			if (page > pCount)
			{
				return pCount;
			}
			else
			{
				return page;
			}
	}
	
	
	
}
